//
//  ShoppingMallVc.m
//  MJApp
//
//  Created by John on 15/3/31.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "ShoppingMallVc.h"
#import "ShoppingMallCell.h"
#import "AuthorizationSearchVc.h"
#import "ProductInfoViewController.h"

@interface ShoppingMallVc ()<UITableViewDataSource,UITableViewDelegate>
{

    NSInteger _page;
    NSString * _order_price;
    NSString * _order_time;
    NSString * _sale_num;
    
}
@property (weak, nonatomic) IBOutlet UIButton *fiterbtnNew;
@property (weak, nonatomic) IBOutlet UIButton *fiterbtnNum;
@property (weak, nonatomic) IBOutlet UIButton *fiterbtnPrice;
@property (nonatomic, strong) NSMutableArray * goodsArr;
@property (weak, nonatomic) IBOutlet UITableView *tableiview;
@property (nonatomic,strong)UIView *priceSelectBackView;

@end

@implementation ShoppingMallVc
//- (void)viewWillAppear:(BOOL)animated{
//
//     [((TabBarController *)self.tabBarController) setTabbarHidden:NO];
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableiview.backgroundColor = nil;
    [self.rightBtn setTitle:@"授权查询" forState:UIControlStateNormal];
    self.navigationItem.title = @"装备";
    _page = 1;
    _order_price = @"other";
    _order_time = @"asc";
    _sale_num = @"other";
    [self getHotSellGoodsListIsLoadMore:NO];
   
    
    // Do any additional setup after loading the view.
}
- (NSMutableArray *)goodsArr{

    if (_goodsArr==nil) {
        _goodsArr = [NSMutableArray array];
    }
    return _goodsArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.goodsArr.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}
- (UITableViewCell  *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    ShoppingMallCell * cell = [ShoppingMallCell cellForTableview:tableView];
    cell.infoDic = self.goodsArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    ShoppingMallCell * cell = (ShoppingMallCell * )[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellH;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0.01;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"id"] = self.goodsArr[indexPath.row][@"id"];
    [WLHTTPRequest getGoodsDeatailWithParam:param usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
       
        ProductInfoViewController * product = [[ProductInfoViewController alloc] init];
        product.detailDic = resultDictionary[@"data"];
        [self.navigationController pushViewController:product animated:YES];
    } andFailureBlock:^(NSError *resultError) {
        
    }];
    
//    ProductInfoViewController * product = [[ProductInfoViewController alloc] init];
////    product.detailDic = resultDictionary[@"data"];
//     [self.navigationController pushViewController:[[ProductInfoViewController alloc] init] animated:YES];
}
- (IBAction)filterGoods:(UIButton *)sender {
    self.fiterbtnNew.selected = NO;
     self.fiterbtnNum.selected = NO;
     self.fiterbtnPrice.selected = NO;
    
    sender.selected = YES;
    
    if (self.priceSelectBackView != nil) {
        
        self.priceSelectBackView.hidden = YES;
    }
    
    if (sender.tag==0) {
        _order_price = @"other";
        _order_time = @"asc";
        _sale_num = @"other";
        [self.fiterbtnPrice setTitle:@"价格" forState:UIControlStateNormal];
        [self getHotSellGoodsListIsLoadMore:NO];
    
    }else if (sender.tag==1){
    
        _order_price = @"other";
        _order_time = @"other";
        _sale_num = @"asc";
        [self.fiterbtnPrice setTitle:@"价格" forState:UIControlStateNormal];
        [self getHotSellGoodsListIsLoadMore:NO];
    }else if (sender.tag == 2) {
        
        _order_time = @"other";
        _sale_num = @"other";
        CGRect rect = [sender convertRect:sender.frame toView:self.view];
        if (self.priceSelectBackView == nil) {
            
            [self setPriceView:rect];
        }else {
            
            self.priceSelectBackView.hidden = NO;
        }
        
    }
}


- (void)setPriceView:(CGRect)rect {

    CGRect frame = rect;
    frame.origin.y += frame.size.height;
    frame.origin.x = self.view.center.x + frame.size.width / 2;
    frame.size.height *= 2;
    self.priceSelectBackView = [[UIView alloc] initWithFrame:frame];
    [self.view addSubview:self.priceSelectBackView];
    self.priceSelectBackView.layer.borderColor = Color_Back_Gray.CGColor;
    self.priceSelectBackView.backgroundColor = [UIColor whiteColor];
    self.priceSelectBackView.layer.borderWidth = 1;
    
    UIButton *upBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.height)];
    upBtn.tag = 4;
    [upBtn setTitle:@"从低到高" forState:UIControlStateNormal];
    upBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [upBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.priceSelectBackView addSubview:upBtn];
    [upBtn addTarget:self action:@selector(priceBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *downBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, rect.size.height, rect.size.width, rect.size.height)];
    downBtn.tag = 5;
    [downBtn setTitle:@"从高到低" forState:UIControlStateNormal];
    downBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [downBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.priceSelectBackView addSubview:downBtn];
    [downBtn addTarget:self action:@selector(priceBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0.9 * rect.size.width, 1)];
    lineView.center = CGPointMake(CGRectGetWidth(rect) / 2, CGRectGetHeight(rect));
    lineView.backgroundColor = Color_Back_Gray;
    [self.priceSelectBackView addSubview:lineView];
}

- (void)priceBtnAction:(UIButton *)sender {
    
    self.priceSelectBackView.hidden = YES;
    [self.fiterbtnPrice setTitle:[sender titleForState:UIControlStateNormal] forState:UIControlStateNormal];
    
    if (sender.tag == 4) {
        
        //低到高
        _order_price = @"asc";
        
    }else if (sender.tag == 5) {
        
        _order_price = @"desc";
        //高到低
    }
    [self getHotSellGoodsListIsLoadMore:NO];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)clickRightButton{

    AuthorizationSearchVc * authVc = [[AuthorizationSearchVc
                                       alloc] init];
    [self.navigationController pushViewController:authVc animated:YES];
    NSLog(@"授权查询");
}

- (void)getHotSellGoodsListIsLoadMore:(BOOL)isLoadMore{
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"pageSize"] = pageSize;
    param[@"type"]= @(0);
    if (isLoadMore) {
        if (self.goodsArr.count==0) {
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"没有更多商品"];
            return;
        }else{
            
            ++_page;
        }
    }else{
        
        _page = 1;
    }
    param[@"page"] = @(_page);
    param[@"code"] = @"";
    param[@"name"] = @"";
    param[@"size"] = @(0);
    param[@"color_id"] = @(0);
    param[@"is_lock"] = @(-1);
    param[@"order_price"] = _order_price;
    param[@"order_time"] = _order_time;
    param[@"sale_num"] = _sale_num;
    param[@"pageSize"] = @(20);
    [WLHTTPRequest getCommodityListWithParam:param usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        if (!isLoadMore) {
            [self.goodsArr removeAllObjects];
        }
        NSArray * goodsArr = resultDictionary[@"data"];
        if (goodsArr.count==0) {
            if (isLoadMore) {
                [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"没有更多数据"];
                return ;
            }else{
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"暂时没有商品"];
                
            }
        }
        [self.goodsArr addObjectsFromArray:resultDictionary[@"data"]];
        [self.tableiview reloadData];
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

@end
